//
//  ZMPopoverView.m
//  ZMPopoverDemo
//
//  Created by Leo on 15/9/7.
//  Copyright (c) 2015年 Leo. All rights reserved.
//

#import "KKPopoverView.h"

@interface KKPopoverView () {
    
    CGPoint _targetPoint;
    UIView *_pannelView;
    
    BOOL _isAppear;
    
    UIColor *_popoverBackgroundColor;
}

@end

@implementation KKPopoverView

- (instancetype)initWithContentView:(UIView*)contentView {
    
    if (contentView == nil) {
        return nil;
    }
    
    self = [super initWithFrame: CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight([[UIScreen mainScreen] bounds]))];
    if (self) {
        self.hidden = YES;
        self.backgroundColor = [UIColor clearColor];
        
        _popoverBackgroundColor = [UIColor colorWithWhite:0.1 alpha:0.2];
        
        CGFloat contentViewWidth = CGRectGetWidth(contentView.bounds);
        CGFloat contentViewHeight = CGRectGetHeight(contentView.bounds);
        
        _pannelView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, contentViewWidth + 10, contentViewHeight + 10)];
        _pannelView.layer.cornerRadius = 5.0;
        _pannelView.layer.backgroundColor = _popoverBackgroundColor.CGColor;
        [self addSubview: _pannelView];
        
        contentView.center = CGPointMake(CGRectGetWidth(_pannelView.bounds)/2, CGRectGetHeight(_pannelView.bounds)/2);
        [_pannelView addSubview: contentView];
        _contentView = contentView;
        
        _isAppear = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    
    if (CGPointEqualToPoint(_targetPoint, CGPointZero)) {
        return;
    }
    
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(contextRef);
    
    CGPoint points[] = {_targetPoint,CGPointMake(_targetPoint.x + 12, _targetPoint.y + 16),CGPointMake(_targetPoint.x - 12, _targetPoint.y + 16)};
    CGContextAddLines(contextRef, points, 3);
    CGContextClosePath(contextRef);
    
    [_popoverBackgroundColor setFill];
    [[UIColor clearColor] setStroke];
    
    CGContextDrawPath(contextRef, kCGPathFillStroke);

    _pannelView.center = CGPointMake(_targetPoint.x, _targetPoint.y + CGRectGetHeight(_pannelView.bounds)/2 + 16);
    
    [self viewDidAppear];
}


- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    CGPoint coverPoint = [self convertPoint: point toView: _pannelView];
    
    if ([_pannelView pointInside: coverPoint withEvent: event]) {
        
        return [super hitTest: point withEvent: event];
//        return _contentView;
    }
    
    [self viewDidDisappear];
    
    return [super hitTest: point withEvent: event];
}

- (void)viewDidAppear {
 
    if (_isAppear) {
        return;
    }
    
    _isAppear = YES;
    
    __weak typeof(self) weakSelf = self;
    
    weakSelf.alpha = CGFLOAT_MIN;
    weakSelf.hidden = NO;
    [UIView animateWithDuration: 0.25 animations:^{
        weakSelf.alpha = 1.0;
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName: KKPopoverViewAppearNotificationName object: nil];
}

- (void)viewDidDisappear {
    if (!_isAppear) {
        return;
    }
    
    _isAppear = NO;
    
    if (_disappearBlock) {
        _disappearBlock();
    }
    
    
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration: 0.25 animations:^{
        weakSelf.alpha = CGFLOAT_MIN;
    }completion:^(BOOL finished) {
        weakSelf.hidden = YES;
        [weakSelf removeFromSuperview];
    }];
    
    [[NSNotificationCenter defaultCenter] postNotificationName: KKPopoverViewDisappearNotificationName object: nil];
}

#pragma mark - Public methods

- (void)presentPopoverFromPoint:(CGPoint)point {
    
    _targetPoint = point;
    [self setNeedsDisplay];
}

@end
